package pool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.junit.Test;

import java.util.concurrent.*;

/**
 * @author: User
 * @date: 2022/11/10
 * @Description:此类用于xxx
 */
public class InitPool {

    private static final ThreadPoolExecutor pool;

    static {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("test-pool-%d").build();
        pool = new ThreadPoolExecutor(16, 32, 60L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1024), threadFactory, new ThreadPoolExecutor.AbortPolicy());
        pool.allowCoreThreadTimeOut(true);
    }

    public void doWork(Task task) throws ExecutionException, InterruptedException {
        Future res = pool.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                task.print();
                return "";
            }
        });
        res.get();
    }

    @Test
    public void test() throws ExecutionException, InterruptedException {
        long start=  System.currentTimeMillis();
        for (int i = 0; i <=2147483; i++) {
            doWork(new Task(i));
        }
        long end=  System.currentTimeMillis();
        System.out.println((end-start)/1000);
    }
}


